# -*- coding: utf-8 -*-
# @author: wch
# @file: img_to_video.py
# @time: 2023/2/2 15:18

import numpy_learn as np
import cv2
import os


# VideoWriter(对象） write(方法）

def img_to_video(path, width, height):
    "传入保存图片的文件夹路径，已经图片的宽、高，在文件夹的同目录下生成out.mp4视频文件。"

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    vw = cv2.VideoWriter(path[:-5] + "out.mp4", fourcc, 30, (width, height))

    files = os.listdir(path)
    files.sort(key=lambda x: int(x[:-4]))

    for file in files:
        img = cv2.imread(path + "\\" + file)
        vw.write(img)

    vw.release()


if __name__ == '__main__':
    path = r"E:\dyk\opencv\media\image"
    img_to_video(path, 360, 640)
